﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace iHub
{
    public partial class selecttable : Form
    {
        List<string> extables = new List<string>();
        DataTable dtTable = new DataTable();
        string tableName = string.Empty;
        bool DataTables = false;

        public selecttable(List<string> tables)
        {
            InitializeComponent();
            extables = tables;
        }

        private void selecttable_Load(object sender, EventArgs e)
        {
            try
            {
                if (!DataTables)
                {
                    if (extables != null)
                    {
                        foreach (string name in extables)
                        {
                            try
                            {
                                listoftables.Items.Add(name);
                            }
                            catch (Exception err)
                            {
                                MessageBox.Show(err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                            }
                        }
                    }
                }
                else
                {
                    if (dtTable.Rows.Count > 0)
                    {
                        for (int tables = 0; tables < dtTable.Rows.Count; tables++)
                        {
                            try
                            {
                                ListViewItem lv = new ListViewItem();
                                lv.Text = dtTable.Rows[tables][0].ToString();
                                lv.Tag = dtTable.Rows[tables][0];
                                listoftables.Items.Add(lv);
                            }
                            catch (Exception err)
                            {
                                MessageBox.Show(err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                            }
                        }
                    }
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnok_Click(object sender, EventArgs e)
        {
            try
            {
                if (listoftables.Items.Count > 0)
                {
                    if (tableName != string.Empty)
                    {
                        documentselect.tablename = tableName;
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("Select a Table");
                    }
                }
                else
                {
                    this.Close();
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void listoftables_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            tableName = listoftables.SelectedItem.ToString();
        }
    }
}
